//
//  ViewController.m
//  17-HomeWorkContact
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
#import "PersonDetailViewController.h"
#import "AddPersonViewController.h"
#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,AddPersonDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonnull, strong)NSMutableArray<Person *> *contactArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /**使用tableView完成通讯录, 注意联系人使用类创建
    添加联系人 联系人包含 头像 姓名 联系方式 家庭住址
    联系人列表 (注意: 添加完联系人, 列表要刷新)
    删除联系人
    查看联系人详情
    */
    _contactArray = [NSMutableArray array];
    [self handleData];
    self.navigationController.navigationBar.hidden = NO;
// 1.设置leftBarButtonItem为编辑按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
//    2.设置rightBarButtonItem为添加联系人按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加联系人" style:UIBarButtonItemStylePlain target:self action:@selector(addPerson)];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
}
- (void)addPerson:(Person *)person {
    [_contactArray addObject:person];
    [_tableView reloadData];
}

- (void)addPerson {
    AddPersonViewController *addPersonVC = [[AddPersonViewController alloc] init];
    addPersonVC.addPersonDelegate = self;
    [self.navigationController pushViewController:addPersonVC animated:YES];
}


//2.让tableView出狱编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing: editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
// 3.询问tableView是否能够编辑(默认为YES)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 4 返回编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// 5.提交编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
   //先删除数据
        [_contactArray removeObjectAtIndex:indexPath.row];
   //删除cell
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
// UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _contactArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.textLabel.text = _contactArray[indexPath.row].name;
    return cell;
}
//点击跳转到详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonDetailViewController *personDetail = [[PersonDetailViewController alloc] init];
    personDetail.person = _contactArray[indexPath.row];
    [self.navigationController pushViewController:personDetail animated:YES];
}
- (void)showPersonDetail:(Person *)person {
    
}

//处理数据
- (void)handleData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
   NSMutableArray *dataArray = [NSMutableArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in dataArray) {
        Person *person = [[Person alloc] initWithName:[dic objectForKey:@"name"] imageName:@"01" phoneNumber:[dic objectForKey:@"phoneNumber"] address:nil];
        [_contactArray addObject:person];
    }
    NSLog(@"%ld", _contactArray.count);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
